﻿using SuperSocket.SocketBase;
using SuperSocket.SocketBase.Protocol;
using SuperWebSocket;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json;
using IOA.MES.WebTool.WebSocketSupport.CommandModels;

namespace IOA.MES.WebTool.WebSocketSupport
{
    public class MesToolServer : WebSocketServer<MesToolSession>
    {
        protected override void OnStartup()
        {
            this.NewMessageReceived += MesToolServer_NewMessageReceived;
            base.OnStartup();
        }

        private void MesToolServer_NewMessageReceived(MesToolSession session, string value)
        {
            var messageModel = JsonConvert.DeserializeObject<MesToolMessageModel<object>>(value);
            var handler = new MesToolMsgHandler { Session = session };
            switch (messageModel.Command)
            {
                case "Login"://登录
                    handler.Login(JsonConvert.DeserializeObject<MesToolMessageModel<LoginModel>>(value).Body);
                    break;
                case "PrintCodes"://分片打印
                    handler.PrintCodes(JsonConvert.DeserializeObject<MesToolMessageModel<PrintModel>>(value).Body);
                    break;
                case "PrintPack"://唛头打印
                    handler.PrintPack(JsonConvert.DeserializeObject<MesToolMessageModel<PrintPackModel>>(value).Body);
                    break;
                case "DownloadImages"://下载图片
                    handler.DownloadImages(JsonConvert.DeserializeObject<MesToolMessageModel<DownloadImagesModel>>(value).Body);
                    break;
            }
        }

        protected override void OnStopped()
        {
            base.OnStopped();
        }
        protected override void OnNewSessionConnected(MesToolSession session)
        {
            base.OnNewSessionConnected(session);
        }
        protected override void OnSessionClosed(MesToolSession session, CloseReason reason)
        {
            base.OnSessionClosed(session, reason);
        }
    }
}
